/**
 * 
 */
package trungsi.gea.test;

//import java.io.File;

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

import trungsi.gea.photos.jdo.PersistenceManagerHolder;

//import com.google.appengine.api.datastore.dev.LocalDatastoreService;
//import com.google.appengine.tools.development.ApiProxyLocalImpl;
//import com.google.apphosting.api.ApiProxy;

/**
 * @author trungsi
 *
 */
@ContextConfiguration(locations="/test-application-context.xml")
public class BaseTestCase extends AbstractTestNGSpringContextTests {
	
	private final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
	
	protected PersistenceManagerHolder pmHolder = new PersistenceManagerHolder();
	
	@BeforeMethod
	public void beforeMethod() {
		/*ApiProxy.setEnvironmentForCurrentThread(new TestEnvironment());
		ApiProxy.setDelegate(new ApiProxyLocalImpl(new File(".")){});
		
		ApiProxyLocalImpl proxy = (ApiProxyLocalImpl) ApiProxy.getDelegate();
        proxy.setProperty(LocalDatastoreService.NO_STORAGE_PROPERTY, Boolean.TRUE.toString());
        */
		helper.setUp();
		pmHolder.open();
	}
	
	@AfterMethod
	public void afterMethod() {
		pmHolder.close();
		
		/*ApiProxyLocalImpl proxy = (ApiProxyLocalImpl) ApiProxy.getDelegate();
        LocalDatastoreService datastoreService = (LocalDatastoreService) proxy.getService("datastore_v3");
        datastoreService.clearProfiles();
        
		ApiProxy.setDelegate(null);
		ApiProxy.setEnvironmentForCurrentThread(null);*/
		helper.tearDown();
	}
	
	protected final void newTransaction() {
		// transaction does not work yet on local db
		//pmHolder.newTransaction();
	}
}
